// pages/blueWifi/blueWifi.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipFlag: false,
    wifiInfo: "",
    macs: [],
    ssid: "",
    bssid: "",
    uuid: "",
    macRssi: "",
    password: "",
    eyeFlag: true,
    showProgress: false,
    configFlag: false,
    selectList:[],
    bssidList: [],
    errInputList:[]
  },
  handleEyePassword() {
    this.setData({
      eyeFlag: !this.data.eyeFlag
    })
  },
  initWifi: function () {
    var self = this;
    self.setData({
      password: ""
    })
    wx.startWifi();
    wx.getConnectedWifi({
      success: function (res) {
        if (res.wifi.SSID.indexOf("5G") != -1) {
          self.wifiPrompt();
        }
        self.setData({
          wifiInfo: res.wifi,
          bssidList : res.wifi.BSSID.split(':')
        });
        let MeshID = self.data.wifiInfo.BSSID
        let selectId = (util.ex16hex(MeshID) + 1).toString(16)
        let selectMeshId = util.strInsert(selectId, 2,":")
        self.setData({
          selectList :[MeshID,selectMeshId]
        })
        // console.log("get");
        self.getStorage();
      },
      fail: function (res) {
        console.log(res);
        self.setData({
          wifiInfo: {
            SSID: "null"
          }
        })
      }
    });
    wx.onWifiConnected(function (res) {
      self.setData({
        password: ""
      })
      if (res.wifi.SSID == "") {
        self.setData({
          wifiInfo: {
            SSID: "null"
          }
        })
      } else {
        if (res.wifi.SSID.indexOf("5G") != -1) {
          self.wifiPrompt();
        }
        self.setData({
          wifiInfo: res.wifi,
          bssidList : res.wifi.BSSID.split(':')
        });
        let MeshID = self.data.wifiInfo.BSSID
        let selectId = (util.ex16hex(MeshID) + 1).toString(16)
        let selectMeshId = util.strInsert(selectId, 2,":")
        self.setData({
          selectList :[MeshID,selectMeshId]
        })
        // console.log("on");
        self.getStorage();
      }
      
    })
  },
  getStorage: function () {
    var self = this;
    var data = wx.getStorageSync('WIFIINFO');
    if (!util._isEmpty(data)) {
      for (var i in data) {
        // console.log(self.data.wifiInfo.SSID);
        if (data[i].name == self.data.wifiInfo.SSID) {
          self.setData({
            password: data[i].password
          })
          return false;
        }
      }
    }
  },
  wifiPrompt: function () {
    wx.showToast({
      title: '当前为5G网络，请切换为2.4G网络进行配网',
      icon: 'none',
      duration: 3000
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  bindViewConnect: function () {
    var self = this;
    let list = this.data.bssidList
    this.setData({
      errInputList:[]
    })
    let errList = this.data.errInputList
    list.forEach((item,index)=>{
      if(item==""){
        errList.push(index)
      }
    })
    this.setData({
      errInputList:errList
    })
    if(errList.length>0){
      return util.showToast('请输入MeshId')
    }
    if (self.data.wifiInfo.BSSID) {
      this.setData({
        showProgress: true
      })
      //   wx.navigateTo({
      //     url: '/pages/blueConnect/blueConnect?macs=' + self.data.macs + "&ssid=" + self.data.wifiInfo.SSID + "&password=" + self.data.password + "&meshId=" + self.data.wifiInfo.BSSID + "&macRssi=" + self.data.macRssi,
      //   })
    } else {
      wx.showToast({
        title: '请链接WIFI网络进行配网',
        icon: 'none',
        duration: 3000
      })
    }

  },
  // 打开自定义设置
  handleConfig() {
    this.setData({
      configFlag: !this.data.configFlag,
      // bssidList : this.data.wifiInfo.BSSID.split(':')
    })
  },
  // 手动编辑meshID
  meshIdEdit(e){
    let index = e.currentTarget.dataset.index
    let value = e.detail.value.replace(/[^0-9a-fA-F:]/g,"");
    value = value.slice(0,2)
    let list = this.data.bssidList
    list[index] = value
    this.setData({
      bssidList:list,
      ["wifiInfo.BSSID"]:list.join(':')
    })
  },
  meshIdBlur(e){
    let list = this.data.bssidList
    this.setData({
      errInputList:[]
    })
    let errList = this.data.errInputList
    list.forEach((item,index)=>{
      if(item==""){
        errList.push(index)
      }
    })
    this.setData({
      errInputList:errList
    })
    if(errList.length>0){
      return util.showToast('请输入MeshId')
    }
  },
  // 下拉选择meshId
  selectMeshId() {
    var self = this
   
    wx.showActionSheet({
      itemList: self.data.selectList,
      success (res) {
        self.setData({
          ["wifiInfo.BSSID"]:self.data.selectList[res.tapIndex],
          bssidList : self.data.selectList[res.tapIndex].split(':')
        })        
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    wx.setNavigationBarTitle({
      title: "确认WiFi"
    });
    self.setData({
      macs: options.macs.split(","),
      macRssi: options.macRssi
    })
    // console.log(self.data.macs)
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) { }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initWifi();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.stopWifi();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})